package com.customization.jucailinkq.workflow;

import com.engine.core.cfg.annotation.CommandDynamicProxy;
import com.engine.core.interceptor.AbstractCommandProxy;
import com.engine.core.interceptor.Command;
import com.engine.jucailinkq.attendance.workflow.proxy.tactics.WorkFlowHandleTacis;
import com.engine.jucailinkq.common.util.DbTools;
import com.engine.jucailinkq.common.util.Utils;
import com.engine.workflow.cmd.monitor.DoDeleteCmd;
import com.engine.workflow.cmd.monitor.DoRepossessedCmd;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.workflow.workflow.RequestForceDrawBack;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 流程删除代理类
 */
@CommandDynamicProxy(target = DoDeleteCmd.class, desc="流程删除")
public class KQDoDeleteProxyCmd extends AbstractCommandProxy<Map<String,Object>> {
    private List<WorkFlowHandleTacis> rulers;
    {
        rulers = new Utils<WorkFlowHandleTacis>().getAllClassByInterface(WorkFlowHandleTacis.class);

    }
    @Override
    public Map<String, Object> execute(Command<Map<String, Object>> command) {

        BaseBean bb = new BaseBean();
        bb.writeLog("KQDoDeleteProxyCmd start");
        DoDeleteCmd doDeleteCmd = (DoDeleteCmd)command;
        Map<String, Object> params = doDeleteCmd.getParams();

        String multiRequestIds = params.get("multiRequestIds").toString();




        Map<String, Object> resultMap = nextExecute(command);



        String sql = "select requestid,workflowid from workflow_requestbase_dellog where requestid in ("+multiRequestIds+")";
        List<Map<String,Object>> dataList = DbTools.getSqlToList(sql);
        bb.writeLog("KQDoDeleteProxyCmd requestId"+dataList.toString());

        sql = "select ljid,lclx,lcbd  from uf_jcl_kq_kqlcsz where zt=0";
        List<Map<String,Object>> list = DbTools.getSqlToList(sql);
        Map<String,List<Map<String,Object>>> groupByMap = list.stream().collect(Collectors.groupingBy(e->e.get("ljid").toString()));

        for (Map<String,Object> dataMap:dataList){
            String requestid = dataMap.get("requestid").toString();
            String workflowid = dataMap.get("workflowid").toString();
            if (groupByMap.get(workflowid) != null && groupByMap.get(workflowid).size()>0){
                Map<String,Object> map = groupByMap.get(workflowid).get(0);
                String workflowType = map.get("lclx").toString();
                for (WorkFlowHandleTacis workFlowHandleTacis :rulers){
                    if (workFlowHandleTacis.support(workflowType)){
                        map.put("requestId",requestid);
                        map.put("operate","dodelete");
                        workFlowHandleTacis.reset(map);
                    }
                }
            }
        }
        bb.writeLog("KQDoDeleteProxyCmd requestMap"+resultMap.toString());
        return resultMap;
    }
}
